﻿#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>
#include <QStringLiteral>
ClientWidget::ClientWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClientWidget)
{
    ui->setupUi(this);

    tcpSocket = NULL;

    //分配空间，指定父对象
    tcpSocket = new QTcpSocket(this);

    setWindowTitle(QStringLiteral("客户端"));

    /*************************************************
      Description:    连接服务器
      Called By:
      Auther:         闫世伦
      Time:           2021-01-31 16:31:48
    *************************************************/
    connect(tcpSocket, &QTcpSocket::connected,
            [=]()
            {
                ui->textEditRead->setText(QStringLiteral("成功和服务器建立好连接"));
            }
            );
    /*************************************************
      Description:    获取对方发送的信息
      Auther:         闫世伦
      Time:           2021-01-31 16:31:48
    *************************************************/
    connect(tcpSocket, &QTcpSocket::readyRead,
            [=]()
            {
                //获取对方发送的内容
                QByteArray array = tcpSocket->readAll();
                //追加到编辑区中
                ui->textEditRead->append(array);
            }

            );
}

ClientWidget::~ClientWidget()
{
    delete ui;
}
/*************************************************
  Description:    与服务器进行连接
  return:         void
  Called By:
  Input:          clicked
  Auther:         闫世伦
  Time:           2021-01-31 16:31:48
*************************************************/
void ClientWidget::on_buttonConnect_clicked()
{
    //获取服务器ip和端口
    QString ip = ui->lineEditIP->text();
    qint16 port = ui->lineEditPort->text().toInt();

    //主动和服务器建立连接
    tcpSocket->connectToHost(QHostAddress(ip), port);
}

/*************************************************
  Description:    向客户端发送数据
  return:         void
  Called By:
  Input:          clicked
  Auther:         闫世伦
  Time:           2021-01-31 16:31:48
*************************************************/
void ClientWidget::on_buttonSend_clicked()
{
    //获取编辑框内容
    QString str = ui->textEditWrite->toPlainText();
    //发送数据
    tcpSocket->write( str.toUtf8().data() );
}

/*************************************************
  Description:    断开服务器连接
  return:         void
  Called By:
  Input:          clicked
  Auther:         闫世伦
  Time:           2021-01-31 16:31:48
*************************************************/
void ClientWidget::on_buttonClose_clicked()
{
    //主动和对方断开连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
}
